package com.example.swagger.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2    // 开启Swagger2
public class SwaggerConfig {

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }

    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }


    // 配置了Swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){

        // 设置显示的Swagger环境
        Profiles profile = Profiles.of("dev","test");
        //通过environment.acceptProfiles判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profile);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("咸鱼")
                .enable(flag) // enable是否启动Swagger，如果为False,则Swagger不能再浏览器中访问
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.swagger.controller"))
//                .paths(PathSelectors.ant("/sms/**"))
                .build();
    }

    // 配置Swagger信息：apiInfo
    private ApiInfo apiInfo(){

        // 作者信息
        Contact contact =  new Contact("小小黑", "https://baidu.com", "2832489281@qq.com");

        return new ApiInfo(
                "咸鱼的SwaggerAPI文档",
                "咸鱼王中王",
                "v1.0",
                "http://localhost:8080/index.html",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }
}
